/**
 *
 * @author
 *
 */
class OpStage extends egret.DisplayObjectContainer {

    private _currPageName: string;
    private _touchPage: egret.DisplayObject;

    private _pageTable: Object = {};

    private _hidePage(): void {
        egret.Tween.get(this).to({ alpha: 0 },1000,egret.Ease.cubicInOut).call(function() {
            this.removeChild(this._pageTable[this._currPageName]);
        });
    }

    private _showPage(targetPageName: string): void {
        var targetPage: OpPage = this._pageTable[targetPageName];

        targetPage.reset();

        this.addChild(targetPage);
        this._currPageName = targetPageName;

        egret.Tween.get(this).to({ alpha: 1 },1000,egret.Ease.cubicInOut).call(function() {
            targetPage.play(function() {
                targetPage.enableTouch();
            },this);
        });
    }

    private _switchTo(targetPageName: string): void {

        var targetPage: OpPage = this._pageTable[targetPageName],
            currPage: OpPage;

        targetPage.reset();

        if(this._currPageName) {
            currPage = this._pageTable[this._currPageName];
            this.addChildAt(targetPage,this.getChildIndex(currPage));
            currPage.hide(function() {
                this.removeChild(currPage);
                targetPage.play(function() {
                    targetPage.enableTouch();
                },this);
            },this);
        } else {
            this.addChild(targetPage);
            setTimeout(function() {
                targetPage.play(function() {
                    targetPage.enableTouch();
                },this);
            },100);
        }

        this._currPageName = targetPageName;
    }

    public constructor() {
        super();

        this._pageTable['op-1'] = new OpPageBasic('page/op-1.jpg','page/w-1.png');
        this._pageTable['op-2'] = new OpPageBasic('page/op-2.jpg','page/w-2.png');
        this._pageTable['op-3'] = new OpPageBasic('page/op-3.jpg','page/w-3.png');
        this._pageTable['op-guanyu']     = new OpPageBasic('page/op-guanyu.jpg','page/w-guanyu.png');
        this._pageTable['op-yanhou']     = new OpPageBasic('page/op-yanhou.jpg','page/w-yanhou.png');
        this._pageTable['op-shengqishi'] = new OpPageBasic('page/op-shengqishi.jpg', 'page/w-shengqishi.png');
        this._pageTable['op-select'] = new OpPageSelect();
        this._pageTable['op-fire'] = new OpPageFire();
        this._pageTable['op-fire-again'] = new OpPageFireAgain();
        this._pageTable['op-xiazai'] = new OpPageXiazai();

        this._switchTo('op-1');

        EventBus.on('OP_PLAY_NEXT',function() {
            if(this._currPageName === 'op-1') {
                this._switchTo('op-2');
            } else if(this._currPageName === 'op-2') {
                this._switchTo('op-3');
            } else if(this._currPageName === 'op-3') {
                this._switchTo('op-select');
            } else if(this._currPageName === 'op-guanyu') {
                this._hidePage();
                EventBus.trigger('OP_PLAY_SCENE',0);
            } else if(this._currPageName === 'op-yanhou') {
                this._hidePage();
                EventBus.trigger('OP_PLAY_SCENE',1);
            } else if(this._currPageName === 'op-shengqishi') {
                this._hidePage();
                EventBus.trigger('OP_PLAY_SCENE',2);
            } else if(this._currPageName === 'op-xiazai') {
                this._switchTo('op-select');
            } else {
                // Do nothing
            }
        },this);

        EventBus.on('OP_PLAY_ROLE',function(type) {
            if(type === 0) {
                this._switchTo('op-guanyu');
            } else if(type === 1) {
                this._switchTo('op-yanhou');
            } else if(type === 2) {
                this._switchTo('op-shengqishi');
            } else {
                // Dothing
            };
        },this);

        EventBus.on('GAME_FIRE_READY',function() {
            this._showPage('op-fire');
        },this);

        EventBus.on('OP_FIRE_FIRST',function() {
            this._hidePage();
        },this);

        EventBus.on('GAME_HALF_DONE',function() {
            this._showPage('op-fire-again');
        },this);

        EventBus.on('OP_FIRE_SECOND',function() {
            this._hidePage();
        },this);

        EventBus.on('GAME_ALL_DONE',function() {
            this._showPage('op-xiazai');
        },this);

        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,function(event: egret.TouchEvent) {
            if(this._currPageName) {
                this._pageTable[this._currPageName].oneTouch(event);
            }
        },this);
    }

}
